package cn.yuanqiao.web.controller.archive.danganzhengli;

import java.io.*;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.archive.Identify.domain.TenantYqJdFile;
import cn.yuanqiao.archive.danganzhengli.domain.ArchivePack;
import cn.yuanqiao.archive.danganzhengli.domain.PackDetail;
import cn.yuanqiao.archive.danganzhengli.service.IArchivePackService;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 打包日志Controller
 * 
 * @author xy
 * @date 2024-09-13
 */
@RestController
@RequestMapping("/archive/pack")
public class ArchivePackController extends BaseController
{
    @Autowired
    private IArchivePackService archivePackService;

    /**
     * 查询打包日志列表
     */
//    @RequiresPermissions("archive:pack:list")
    @GetMapping("/list")
    public TableDataInfo list(ArchivePack archivePack)
    {
        startPage();
        List<ArchivePack> list = archivePackService.selectArchivePackList(archivePack);
        return getDataTable(list);
    }

    /**
     * 导出打包日志列表
     */
//    @RequiresPermissions("archive:pack:export")
    @Log(title = "打包日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchivePack archivePack)
    {
        List<ArchivePack> list = archivePackService.selectArchivePackList(archivePack);
        ExcelUtil<ArchivePack> util = new ExcelUtil<ArchivePack>(ArchivePack.class);
        util.exportExcel(response, list, "打包日志数据");
    }

    /**
     * 获取打包日志详细信息
     */
//    @RequiresPermissions("archive:pack:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(archivePackService.selectArchivePackById(id));
    }

    /**
     * 新增打包日志
     */
//    @RequiresPermissions("archive:pack:add")
    @Log(title = "打包日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchivePack archivePack)
    {
        return toAjax(archivePackService.insertArchivePack(archivePack));
    }

    /**
     * 修改打包日志
     */
//    @RequiresPermissions("archive:pack:edit")
    @Log(title = "打包日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArchivePack archivePack)
    {
        return toAjax(archivePackService.updateArchivePack(archivePack));
    }

    /**
     * 删除打包日志
     */
//    @RequiresPermissions("archive:pack:remove")
    @Log(title = "打包日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(archivePackService.deleteArchivePackByIds(ids));
    }


    /**
     * 打包
     */
    @Log(title = "打包", businessType = BusinessType.OTHER)
    @PostMapping("/pack")
    public AjaxResult pack(@RequestBody JSONObject jsonObject){
        Long categoryId=jsonObject.getLong("categoryId");
        String[]idStr= jsonObject.getString("ids").split(",");
        Long[]ids= Arrays.stream(idStr)
                .map(Long::valueOf)
                .toArray(Long[]::new);

        Integer type=jsonObject.getInteger("type");
        archivePackService.pack(categoryId,ids,type);
        return AjaxResult.success();
    }
   //详情
   @Log(title = "档案详情", businessType = BusinessType.SELECT)
   @PostMapping("/detailList")
   public TableDataInfo detailList(@RequestParam("pId") Long pId)
   {
       startPage();
       List<PackDetail> list = archivePackService.selectDetailList(pId);
       return getDataTable(list);
   }
    //详情
    @Log(title = "包下载", businessType = BusinessType.SELECT)
    @PostMapping("/download")
    public void downLode(@RequestParam("id") Long id, HttpServletResponse response) throws IOException {
        archivePackService.downLode(id,response);
    }



}
